package team.yidong.fenda.controller;

import com.sun.javafx.sg.prism.NGShape;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.yidong.fenda.entity.Answer;
import team.yidong.fenda.entity.Question;
import team.yidong.fenda.entity.QuestionURL;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.service.QuestionService;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuanlong on 2017/10/27.
 */
@Controller
public class QuestionController {
    @Autowired
    QuestionService questionService;
    @Autowired
    HospitalService hospitalService;

    @RequestMapping("/question")
    public String question(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);
        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
            model.addAttribute("questionClass", questionService.findQuestionClassByHospital(hospitalId));
        } else {
            model.addAttribute("questionClass", questionService.findQuestionClassByHospital(hospitalId));
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
        }

        return "question";
    }

    /**
     * 删除问题界面
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/liuxuanlongquestion")
    public String question1(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);
        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
            model.addAttribute("questionClass", questionService.findQuestionClassByHospital(hospitalId));
        } else {
            model.addAttribute("questionClass", questionService.findQuestionClassByHospital(hospitalId));
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
        }

        return "question1";
    }

    @RequestMapping("/findQuestion")
    @ResponseBody
    public JSONObject findQuestion(Model model, HttpSession session, String isSolved, String haveImg, String questionClass, String userName, String askDate, String hospitalName, String questionMoney, String maxDate, String questionContent, String answerStatus, @RequestParam Integer offset, @RequestParam Integer limit) {

        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");

        return questionService.findQuestion(hospitalId, hospitalName, userName, isSolved, haveImg, questionClass, askDate, questionMoney, maxDate, questionContent, answerStatus, offset, limit);
    }



    @RequestMapping("/questionInfo")
    @ResponseBody
    public JSONObject questionInfo(@RequestParam Integer questionId) {
        return questionService.questionInfo(questionId);
    }

    /**
     * 删除问题
     *
     * @param questionId
     * @return
     */
    @RequestMapping("/deleteQuestion")
    @ResponseBody
    public void deleteQuestion(@RequestParam Integer questionId) {
         questionService.deleteQuestion(questionId);
    }
}
